package com.zhuoyue.web.controller;

import com.zhuoyue.biz.core.api.domain.TblPasswordShare;
import com.zhuoyue.biz.core.api.service.TblPasswordShareService;
import com.zhuoyue.common.utils.AesUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author wjming
 * @ClassName: WebController
 * @Description: 锁分享页面
 * @Date 2021-07-02 9:20
 */
@Controller
public class WebController {
    @Autowired
    private TblPasswordShareService tblPasswordShareService;
    /**
     * 临时密码分享类型
     */
    private static final String PASSWORD_SHARE_TYPE_ONCE = "once";
    /**
     * 时效密码分享类型
     */
    private static final String PASSWORD_SHARE_MANY_TIMES = "many_times";
    /**
     * 失效
     */
    private static final String PASSWORD_SHARE_STATE_1 = "1";
    /**
     * 有效
     */
    private static final String PASSWORD_SHARE_STATE_0 = "0";

    /**
     * 健康检查测试页面
     *
     * @return
     */
    @GetMapping("/")
    public String index() {
        return "index";
    }

    /**
     * APP下载页面
     *
     * @return
     */
    @GetMapping("/download")
    public String download() {
        return "download";
    }


    /**
     * 锁分享页面
     *
     * @param id
     * @return
     */
    @GetMapping("/lock")
    public ModelAndView lock(String id) {
        String decryptId = AesUtils.decrypt(id);
        TblPasswordShare passwordShare = tblPasswordShareService.getById(decryptId);
        String str = "";
        if (passwordShare == null || PASSWORD_SHARE_STATE_1.equals(passwordShare.getState())) {
            str = "lockFail";
        } else if (passwordShare != null && PASSWORD_SHARE_STATE_0.equals(passwordShare.getState())) {
            if (PASSWORD_SHARE_TYPE_ONCE.equals(passwordShare.getType())) {
                str = "lock";
            } else if (PASSWORD_SHARE_MANY_TIMES.equals(passwordShare.getType())) {
                str = "lockPeriod";
            }
        }
        ModelAndView modelAndView = new ModelAndView(str);
        return modelAndView;
    }
}
